<?php
namespace app\platform\controller\terminal;
use app\BaseController;
use app\common\model\ShopDetailModel;
use app\common\model\ShopStoreModel;

class ShopDetail extends BaseController{
    public function show()
    {
        $shop_detail=ShopDetailModel::find($this->request->shopId);
        if(null === $shop_detail){
            $shop_detail = new ShopDetailModel();
            $shop_detail->shop_id=$this->request->shopId;
            $shop_detail->save();
        }
        $detail=[
            'name'=>$this->request->shop->name,
            'logo'=>$shop_detail !== null ? $shop_detail->logo :'',
            'door_license_plate'=>$shop_detail !== null ? $shop_detail->door_license_plate :'',
            'business_hours'=>$shop_detail !== null ? $shop_detail->business_hours :'',
            'tel'=>$shop_detail !== null ? $shop_detail->tel :'',
            'address'=>$shop_detail !== null ? $shop_detail->address :'',
            'info'=>$shop_detail !== null ? $shop_detail->info :'',
            'lng'=>$shop_detail !== null ? $shop_detail->lng :'',
            'lat'=>$shop_detail !== null ? $shop_detail->lat :'',
        ];
        return $this->success(['detail'=>$detail]);

    }

    public function save()
    {
        $name=$this->request->param('name');
        $logo=$this->request->param('logo');
        $door_license_plate=$this->request->param('door_license_plate');
        $business_hours=$this->request->param('business_hours');
        $tel=$this->request->param('tel');
        $address=$this->request->param('address');
        $info=$this->request->param('info');
        $lng=$this->request->param('lng');
        $lat=$this->request->param('lat');
        if(empty($name)){
            return $this->error('名称必须填写');
        }
        if(empty($logo)){
            return $this->error('请上传logo');
        }
        if(empty($door_license_plate)){
            return $this->error('请上传门店门牌照');
        }
        if(empty($business_hours)){
            return $this->error('请填写营业时间');
        }
        if(empty($tel)){
            return $this->error('请填写门店电话');
        }
        if(empty($address)){
            return $this->error('请填写门店地址');
        }
        if(empty($info)){
            return $this->error('请填写门店说明');
        }
        if(empty($lng) || empty($lat)){
            return $this->error('请选择门店定位');
        }

        $this->request->shop->name=$name;
        $this->request->shop->save();
        if(null === $shop_detail = ShopDetailModel::find($this->request->shopId)){
            $shop_detail=new ShopDetailModel();
            $shop_detail->shop_id=$this->request->shopId;
        }
        $pre_logo=$shop_detail->logo;
        $pre_door_license_plate=$shop_detail->door_license_plate;
        $shop_detail->logo=$logo;
        $shop_detail->door_license_plate=$door_license_plate;
        $shop_detail->business_hours=$business_hours;
        $shop_detail->tel=$tel;
        $shop_detail->address=$address;
        $shop_detail->info=$info;
        $shop_detail->lng=$lng;
        $shop_detail->lat=$lat;
        $res = $shop_detail->save();
        // 保存成功后将数据写入门店表中并标识为总部店铺
        if($res) {
            $where = [
                ['shop_id','=',$this->request->shopId],
                ['is_main','=',1]
            ];
            // 门店表是否不存在已经存在
             if(null === $shop_store = ShopStoreModel::where($where)->find()){
                $shop_store=new ShopStoreModel();
                $shop_store->shop_id=$this->request->shopId;
                $shop_store->is_main=1;
                $shop_store->enable =1;
            }
            $shop_store->name = $name;
            $shop_store->logo=$logo;
            $shop_store->door_license_plate=$door_license_plate;
            $shop_store->business_hours=$business_hours;
            $shop_store->tel=$tel;
            $shop_store->address=$address;
            $shop_store->info=$info;
            $shop_store->lng=$lng;
            $shop_store->lat=$lat;
            $shop_store->save();
        }
        cache('miniapp_'.$this->request->shopId,null);
        imgReplaceAct($logo,$pre_logo);
        imgReplaceAct($door_license_plate,$pre_door_license_plate);

        return $this->success('编辑成功');

    }
}
